<?php

class My_DataBase_Finder_Results implements Countable, Iterator
{
    
    /**
     * Iterable contents
     * @var array
     */
    protected $contents = array();

    /**
     * @var string
     */
    protected $returnType = My_DataBase_Finder::RETURN_TYPE_ARRAY;

    /**
     * @see Factory
     */
    protected function  __construct()
    {
    }

    /**
     * @return My_DataBase_Finder_Results
     */
    public static function factory()
    {
        return new self();
    }
    
    
    /**
     * @param array $contents
     * @return My_DataBase_Finder_Results 
     */
    public function setContents($contents)
    {
        $this->contents = $contents;
        return $this;   
    }
    
    /**
     * @param boolean $jsonEncoded
     * @return array
     */
    public function getContents($jsonEncoded = false)
    {
        if (true == $jsonEncoded) return Zend_Json::encode ($this->contents);
        return $this->contents;
    }

    /**
     * @param string $type
     * @return My_DataBase_Finder_Results
     */
    public function setReturnType($type)
    {
        $this->returnType = $type;
        return $this;
    }

    /**
     * @see Countable
     * @return integer
     */
    public function count()
    {
        return count($this->contents);
    }

    /**
     * @see Iterator
     * @return mixed
     */
    public function current()
    {
        $value = current($this->contents);
        if (My_DataBase_Finder::RETURN_TYPE_ARRAY != $this->returnType)
        {
            $value = call_user_func($this->returnType.'::loadArray', $value);
        }
        return $value;
    }

    /**
     * @see Iterator
     * @return string
     */
    public function key()
    {
        return key($this->contents);
    }

    /**
     * @see Iterator
     * @return mixed
     */
    public function next()
    {
        return next($this->contents);
    }

    /**
     * @see Iterator
     * @return boolean
     */
    public function rewind()
    {
        return 0;
    }

    /**
     * @see Iterator
     * @return boolean
     */
    public function valid()
    {
        return !is_null(key($this->contents));
    }

}
